import 'reflect-metadata'
import { Connection, createConnection, getConnection } from 'typeorm'
import { Article, User, UserAuth, Comment, Tag } from './entity'

const host = process.env.DATABASE_HOST;
const port = Number(process.env.DATABASE_PORT);
const username = process.env.DATABASE_USERNAME;
const password = process.env.DATABASE_PASSWORD;
const database = process.env.DATABASE_NAME;

let connectionReadyPromise: Promise<Connection> | null = null

export const prepareConnection = () => {
  // 初始化 connectionReadyPromise
  if (!connectionReadyPromise) {
    connectionReadyPromise = (
      async () => {
        try {
          const staleConnection = getConnection()
          await staleConnection.close();
        } catch (error) {
          console.log(error)
        }

        const connection = await createConnection({
          type: 'mysql',
          host,
          port,
          username,
          password,
          database,
          entities: [User, UserAuth, Article, Comment, Tag],
          synchronize: false,
          logging: true,
        })

        return connection
      }
    )()
  }

  // 返回一个 promise 
  return connectionReadyPromise
}
